package com.moliku.insurance.common.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Jackson 工具
 * @author way
 * @date 2020-10-12
 */
public class JacksonUtils {

  /**
   *json转换为obj
   *@param jsonStr 返回数据
   *@param clazz 转换类型
   *@return
   *@throws
   *@author way
   *@date 2020-10-12 13:10
   */
  public static <T> T jsonToObj(String jsonStr,Class<T> clazz){
    ObjectMapper mapper = new ObjectMapper();
    try {
      return  mapper.readValue(jsonStr, clazz);
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }

  /**
   *json转换为List<T>
   *@param jsonStr 返回数据
   *@param clazz 泛型类型
   *@return
   *@throws
   *@author way
   *@date 2020-10-12 13:10
   */
  public static <T> List<T> jsonToObjs(String jsonStr,Class<T> clazz) {
    ObjectMapper mapper = new ObjectMapper();
    JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, clazz);
    try {
      return   (List<T>)mapper.readValue(jsonStr, javaType);
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }


  /**
   *Obj转json
   *@param obj 对象
   *@return
   *@throws
   *@author way
   *@date 2020-10-12 13:10
   */
  public static String objToJson(Object obj){
    ObjectMapper mapper = new ObjectMapper();
    try {
      return mapper.writeValueAsString(obj);
    } catch (JsonProcessingException e) {
      e.printStackTrace();
      return "";
    }
  }


  /**
   *Obj转Map
   *@param obj 对象
   *@return
   *@throws
   *@author way
   *@date 2020-10-12 13:10
   */
  public static Map<String,Object> objToMap(Object obj){
    if(null==obj){
      return null;
    }

    ObjectMapper oMapper = new ObjectMapper();
    return oMapper.convertValue(obj, Map.class);
  }

  /**
   *list object转为list map
   *@param objs 列表
   *@return
   *@throws
   *@author way
   *@date 2020-10-12 13:10
   */
  public static List<Map<String,Object>> objsToMaps(List objs){
    if(CollectionUtils.isEmpty(objs)){
      return null;
    }

    List<Map<String,Object>> list=new ArrayList<>();
    for(Object obj:objs){
      list.add(objToMap(obj));
    }

    return list;
  }

  /**
   *map转对象
   *@param map map
   *@return
   *@throws
   *@author way
   *@date 2020-10-12 13:10
   */
  public static <T> T mapToObj(Map<String,String> map,Class<T> clazz){

    if(null==map|| CollectionUtils.isEmpty(map.keySet())){
      return null;
    }

    ObjectMapper objectMapper = new ObjectMapper();
    return objectMapper.convertValue(map, clazz);
  }


  public static Map<String,String> remove(Map<String, String> params, List<String> keys){
    if(CollectionUtils.isEmpty(keys)||null==params){
      return params;
    }
    Map<String, String> map=new HashMap<>();
    for(Map.Entry<String,String> entry:params.entrySet()){
      if(!keys.contains(entry.getKey())){
        map.put(entry.getKey(),entry.getValue());
      }
    }

    return map;
  }
}
